import OSS from 'ali-oss'

//  上传图片
export function uploadImage (params, options) {
    return new Promise((resolve, reject) => {
        const fileName = params.file.uid + params.file.name
        // 创建实例
        const client = new OSS({
            accessKeyId: 'LTAI5tHzmrQah8RKc82bWzLt',
            accessKeySecret: 'sP0GsjdVpT6wEGUp1aZJcHPZyVl1kZ',
            endpoint: 'https://oss-cn-beijing.aliyuncs.com',
            bucket: 'sz-plat'
        })
        // 分片上传
        client
            .multipartUpload(fileName, params.file, options)
            .then(result => {
                // console.log(result, "result");
                if (result.res.statusCode === 200) {
                    resolve(result.res)
                } else {
                    this.$message.error('上传出错')
                }
            })
            .catch(err => {
                console.log(err, 'err')
                reject(err)
            })
    })
}

//  上传图片2
export function uploadImage1 (params, options) {
    return new Promise((resolve, reject) => {
        const fileName = params.file.lastModified + params.file.name
        // 创建实例
        const client = new OSS({
            accessKeyId: 'LTAI5tHzmrQah8RKc82bWzLt',
            accessKeySecret: 'sP0GsjdVpT6wEGUp1aZJcHPZyVl1kZ',
            endpoint: 'https://oss-cn-beijing.aliyuncs.com',
            bucket: 'sz-plat'
        })
        // 分片上传
        client
            .multipartUpload(fileName, params.file, options)
            .then(result => {
                // console.log(result, "result");
                if (result.res.statusCode === 200) {
                    resolve(result.res)
                } else {
                    this.$message.error('上传出错')
                }
            })
            .catch(err => {
                console.log(err, 'err')
                reject(err)
            })
    })
}

export var onlyClient = null

//  自定义上传
export function customUpload (params) {
    return new Promise((resolve, reject) => {
        if (onlyClient) onlyClient.cancel()
        const fileName = params.lastModified + params.name
        // 创建实例
        const client = new OSS({
            accessKeyId: 'LTAI5tHzmrQah8RKc82bWzLt',
            accessKeySecret: 'sP0GsjdVpT6wEGUp1aZJcHPZyVl1kZ',
            endpoint: 'http://oss-cn-beijing.aliyuncs.com',
            bucket: 'sz-plat'
        })
        onlyClient = client
        // 分片上传
        client
            .multipartUpload(fileName, params)
            .then(result => {
                // console.log(result, 'result');
                if (result.res.statusCode === 200) {
                    resolve(result.res)
                } else {
                    this.$message.error('上传出错')
                }
            })
            .catch(err => {
                console.log(err, 'err')
                reject(err)
            })
    })
}

// 升序 排序
export function compareUp (propertyName, list) {
    if (!list) return []
    // 升序排序
    if (typeof list[0][propertyName] !== 'number') {
    // 属性值为非数字
        return function (object1, object2) {
            var value1 = object1[propertyName]
            var value2 = object2[propertyName]
            return value1.localeCompare(value2)
        }
    } else {
        return function (object1, object2) {
            // 属性值为数字
            var value1 = object1[propertyName]
            var value2 = object2[propertyName]
            return value1 - value2
        }
    }
}

// 关键词高亮
export function highlightKeyWords (list, keyWords) {
    if (!keyWords) return false
    if (!list) return false
    return new Promise((resolve, reject) => {
        const reg = /^\w+$/
        let arr = []
        if (reg.test(keyWords)) {
            arr.push(keyWords)
        } else {
            arr = keyWords.split('')
        }
        console.log(arr)
        arr.forEach(item => {
            list.forEach((ele, index) => {
                const arr = ele.fullNameRed.split(item)
                ele.fullNameRed = arr.join('<span style="color: #F74A4A">' + item + '</span>')
            })
        })
        console.log(list)
        resolve(list)
        return list
    })
}
